package a3;

import java.io.Serializable;

/**
 * <p>Title: CreateEllipse</p>
 * <p>Description: CS 235 Assignment #3</p>
 * @author Erin McNeil
 * @email erin.mcneil@my.uwrf.edu
 * @date 12/2/2011
 * @team Erin, Dara, Alan
 */

/**
 * An ellipse with integer dimensions.
 */
public class CreateEllipse implements Serializable {

	// Private components:

	/*
	 * Variables assigned to each instance of CreateEllipse.
	 */
	private static final long serialVersionUID = 1L;
	protected double major_axis;
	protected double minor_axis;
	protected double Eperimeter;
	protected double Earea;

	// Constructors:

	/**
	 * Create a new Ellipse with the specified major and minor axes. The axes
	 * must be non-negative.
	 * 
	 * @require maj > 0 min > 0
	 * @ensure this.major_axis > 0 this.minor_axis > 0 this.Eperimeter > 0
	 *         this.Earea > 0
	 */
	public CreateEllipse(double maj, double min) {
		assert (maj > 0 && min > 0);
		this.major_axis = maj;
		this.minor_axis = min;
		Eperimeter = perimeter();
		Earea = area();
		assert (this.major_axis > 0);
		assert (this.minor_axis > 0);
		assert (this.Eperimeter > 0);
		assert (this.Earea > 0);
	}

	// Queries:

	/**
	 * The major axis or length of this ellipse.
	 * 
	 *@require
	 *@ensure major_axis > 0
	 * 
	 */
	public double majorAxis() {
		return major_axis;
	}

	/**
	 * The minor axis or width of this ellipse.
	 * 
	 * @require
	 * @ensure minor_axis > 0
	 * 
	 */
	public double minorAxis() {
		return minor_axis;
	}

	/**
	 * The area of this ellipse.
	 * 
	 * @require
	 * @ensure (Math.PI * major_axis * minor_axis / 4) > 0
	 * 
	 */
	public double area() {
		Earea = Math.PI * major_axis * minor_axis / 4;
		return Earea;
	}

	/**
	 * The circumference of this ellipse. We use the approximation from
	 * http://en.wikipedia.org/wiki/Ellipse
	 * 
	 * @require
	 * @ensure perimeter > 0
	 * 
	 */
	public double perimeter() {
		final double a = major_axis / 2.0;
		final double b = minor_axis / 2.0;
		double factor = (a - b) / (a + b);
		factor = 3 * factor * factor;
		Eperimeter = Math.PI * (a + b)
				* (1 + factor / (10 + Math.sqrt(4 - factor)));

		return Eperimeter;
	}

	// toString Method:
	
	/**
	 * ToString method to print out all of the values of the ellipse: The Major
	 * Axis, the Minor Axis, the Perimeter, and the Area. Values have to be
	 * greater than zero.
	 * 
	 * @require this.major_axis > 0 this.minor_axis > 0 this.Eperimeter > 0
	 *          this.Earea > 0
	 * @ensure
	 * 
	 */
	public String toString() {
		assert (this.major_axis > 0);
		assert (this.minor_axis > 0);
		assert (this.Eperimeter > 0);
		assert (this.Earea > 0);
		return "Major Axis: "
				+ (String.format("%.2f", major_axis))
				+ "  Minor Axis: "
				+ (String.format("%.2f", minor_axis))
				+ "  Perimeter: "
				+ (String.format("%.2f", Eperimeter) + "  Area: " + (String
						.format("%.2f", Earea)));
	}

}
